package com.bins.es.service.impl;

import com.bins.es.common.ResultBody;
import com.bins.es.dao.UserRepository;
import com.bins.es.eo.UserModel;
import com.bins.es.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    @Qualifier("userRepository")
    private UserRepository userRepository;

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Override
    public Optional<UserModel> findById(Long id) {
        return userRepository.findById(id);
    }

    @Override
    public ResultBody<UserModel> save(UserModel user) {
        UserModel userModel = userRepository.save(user);
        ResultBody rb = new ResultBody(0, "保存成功!", userModel);
        return rb;
    }

    @Override
    public void delete(UserModel user) {
        userRepository.delete(user);
    }

    @Override
    public List<UserModel> findAll() {
        return (List<UserModel>) userRepository.findAll();
    }

    @Override
    public Page<UserModel> findByAge(Integer age, PageRequest pageRequest) {
        return userRepository.findByAge(age, pageRequest);
    }

    @Override
    public Page<UserModel> findByGender(String gender, PageRequest pageRequest) {
        return userRepository.findByGender(gender, pageRequest);
    }
}
